package com.cy.law.builder.service.impl;

import com.cy.law.builder.service.BuilderService;
import com.cy.law.db.dao.DicGroupDao;
import com.cy.law.db.dao.HumanDao;
import com.cy.law.db.dao.HumanNavItemDao;
import com.cy.law.db.dao.NavItemDao;
import com.cy.law.db.entity.DicGroup;
import com.cy.law.db.entity.Human;
import com.cy.law.db.entity.NavItem;
import com.cy.law.vo.RequestResult;
import com.cy.law.vo.Tree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BuilderServiceImpl implements BuilderService {
    @Autowired
    HumanDao humanDao;
    @Autowired
    DicGroupDao dicGroupDao;
    @Autowired
    NavItemDao navItemDao;
    @Autowired
    HumanNavItemDao humanNavItemDao;

    @Override
    public List<Tree> getHumanTree() {
        List<Tree> myTree = new ArrayList<>();
        Tree root = new Tree(-1, "人员列表");
        root.setIconCls(Tree.TREE_MULTI_HUMAN);
        root.setState(Tree.TREE_STATE_OPEN);
        List<Tree> children = new ArrayList<>();
        List<Human> humans = humanDao.findAll();
        for (Human humanInfo : humans) {
            Tree tmpTree = new Tree();
            tmpTree.setId(humanInfo.getHumanId());
            tmpTree.setText(humanInfo.getHumanName());
            tmpTree.setIconCls(Tree.TREE_HUMAN);
            children.add(tmpTree);
        }
        root.setChildren(children);
        myTree.add(root);
        return myTree;
    }

    @Override
    public RequestResult getHumanInfoByHumanId(int humanId) {
        return RequestResult.new0().setMapData("human", humanDao.findById(humanId).orElse(null));
    }

    @Override
    public List<Tree> getHumanRightTree(int humanId) {
        //先查询出所有的navitem,然后再判断人员是否拥有这个navitem的权限
        List<Tree> children = new ArrayList<>();
        List<DicGroup> groupList = dicGroupDao.findAll();
        for (DicGroup dgi : groupList) {
            Tree tmpTree = new Tree();
            tmpTree.setId(dgi.getGroupId());
            tmpTree.setText(dgi.getGroupNameCh());
            tmpTree.setIconCls(Tree.TREE_UNIT);
            tmpTree.setState(Tree.TREE_STATE_OPEN);
            List<Tree> leafs = new ArrayList<>();
            List<NavItem> navItemList = navItemDao.findAllBySeniorIdAndGroupId(-1, dgi.getGroupId());
            for (NavItem nii : navItemList) {
                Tree leaf = new Tree(nii.getNavId(), nii.getNavNameCh());
                leaf.setIconCls(Tree.TREE_PROCESS);
                //暂时系统只支持2级菜单
                List<NavItem> trueLeafs = navItemDao.findAllBySeniorIdAndGroupId(nii.getNavId(), dgi.getGroupId());
                List<Tree> lastChildren = new ArrayList<>();
                for (NavItem ni : trueLeafs) {
                    Tree trueLeaf = new Tree(ni.getNavId(), ni.getNavNameCh());
                    leaf.setIconCls(Tree.TREE_PROCESS);
                    if (humanNavItemDao.countAllByHumanIdAndNavId(humanId, ni.getNavId()) > 0) {
                        trueLeaf.setChecked(true);
                    } else {
                        trueLeaf.setChecked(false);
                    }
                    lastChildren.add(trueLeaf);
                }
                if (lastChildren.size() > 0) {
                    leaf.setChildren(lastChildren);
                    tmpTree.setState(Tree.TREE_STATE_OPEN);
                } else {
                    int checkFlag = humanNavItemDao.countAllByHumanIdAndNavId(humanId, nii.getNavId());
                    if (checkFlag > 0) {
                        leaf.setChecked(true);
                    }
                }
                leafs.add(leaf);
            }
            tmpTree.setChildren(leafs);
            children.add(tmpTree);
        }
        return children;
    }
}
